"""
文件名:OrderManagementPage.py
作者:陈急
后台订单查询页面表现层.操作层
"""

from Common.Base import Base, open_browser

url = "http://localhost:8080/ecshop/admin/index.php"


class OrderManagementPage(Base):
    """封装订单管理页面操作层"""
    user_loc = ("name", "username")  # 后台用户名输入框
    password_loc = ("name", "password")  # 后台用户输入密码
    activate_alert = ("css selector", "span[onclick='btnCancel(this)']")  # 后台弹窗
    ecshop_loc = ("class name", "btn-tab")  # 点击后台登录账号
    submit_loc = ("class name", "btn-a")  # 点击后台立即登录按钮
    iframe_01 = 'menu-frame'  # 第一层iframe
    iframe_02 = ("css selector", 'html>frameset#frame-body>frameset>frame#main-frame')  # 第二层iframe
    order_man_loc = ("class name", 'icon-order')  # 订单管理div[class=list-div]>table>tbody > tr> td:nth-child(6)
    order_list_loc = ("css selector", "li[id=sub-menu-02_order_list]>a")  # 订单管理-订单查询
    order_number_loc = ("css selector", "form>input[name=order_sn]")  # 订单查询_订单号输入框
    order_people_loc = ("css selector", "form>input[name=consignee]")  # 订单查询_收货人输入框
    order_status_loc = ("css selector", "form>select[name=status]")  # 订单查询-订单状态选择框
    order_submit_loc = ("css selector", "form > input.button")  # 订单查询-搜索按钮
    order_number_list_loc = ("css selector", "#order_0")  # 根据搜索查询的订单编号
    order_status_search_loc = (
    "css selector", "#listDiv > table:nth-child(1) > tbody > tr:nth-child(3) > td:nth-child(6)")  # 查询订单状态

    def input_management_user(self, username):
        """输入用户名"""
        self.send_keys(text=username, locator=self.user_loc)

    def input_management_password(self, password):
        """输入密码"""
        self.send_keys(text=password, locator=self.password_loc)

    def click_remove_alert(self):
        """去除后台登录页面弹窗"""
        self.click(locator=self.activate_alert)

    def click_management_ecshop(self):
        """去除后台登录页面弹窗"""
        self.click(locator=self.ecshop_loc)

    def click_management_login(self):
        """点击后台立即登录"""
        self.click(locator=self.submit_loc)

    def join_order_iframe(self, iframe):
        """进入iframe"""
        self.join_iframe(iframe)  # 进入iframe

    def click_order_management(self):
        """点击订单管理"""
        self.click(locator=self.order_man_loc)  # 点击订单管理

    def click_order_list(self):
        """点击订单列表"""
        self.click(locator=self.order_list_loc)  # 点击订单列表

    def input_order_number(self, number: int):
        """输入订单号码"""
        self.send_keys(text=number, locator=self.order_number_loc)

    def select_order_status(self):
        """订单状态-请选择"""
        self.select_drop_down_menu(index=0, locator=self.order_status_loc)

    def click_order_submit(self):
        """订单查询-搜索"""
        self.click(locator=self.order_submit_loc)

    def get_order_number_management(self):
        """获取查询的订单"""
        return self.get_text(locator=self.order_number_list_loc)

    def get_order_status_management(self):
        """获取所有订单状态列表"""
        return self.get_text(locator=self.order_status_search_loc)

    def quit_order_iframe(self):
        """退出所有iframe"""
        self.quit_iframe()


if __name__ == '__main__':
    import time

    driver = open_browser()  # 打开浏览器
    manage = OrderManagementPage(driver)  # 实例化对象
    manage.open_url(url)  # 打开网址
    manage.clcik_remove_alert()  # 去除弹窗
    manage.click_management_ecshop()  # 点击中间ecshop的登录
    manage.input_management_user("admin")  # 输入用户名
    manage.input_management_password("admin123456")  # 输入密码
    manage.click_management_login()  # 点击立即登录
    time.sleep(5)
    manage.join_order_iframe("menu-frame")  # 第一次进frame
    manage.click_order_management()  # 点击订单管理
    manage.click_order_list()  # 点击订单列表
    manage.quit_order_iframe()  # 退出frame
    manage.join_order_iframe('main-frame')  # 进入新的frame
    manage.input_order_number(2021082848219)  # 输入订单号
    manage.select_order_status()  # 状态选择框选择为全部
    manage.click_order_submit()  # 点击搜索
    manage.get_order_number_management()  # 获取订单号
    manage.get_order_status_management()  # 获取订单状态
